package logic

import (
	"context"
	"zy3/model/mysql"
	"zy3/zy3rpc/zy3rpcclient"

	"zy3/zy3rpc/internal/svc"
	"zy3/zy3rpc/zy3rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListLogic {
	return &ListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListLogic) List(in *zy3rpc.ListRequest) (*zy3rpc.ListResponse, error) {
	// todo: add your logic here and delete this line

	var users []mysql.User
	mysql.DB.Debug().Model(&users).Find(&users)

	var liststruct []*zy3rpcclient.ListStruct

	for _, user := range users {
		List := &zy3rpcclient.ListStruct{
			Id:       int64(user.ID),
			Username: user.Username,
			Age:      user.Age,
			Password: user.Password,
		}
		liststruct = append(liststruct, List)
	}

	return &zy3rpc.ListResponse{List: liststruct}, nil
}
